package com.sinopac.action.RightsManagement;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.sinopac.actionCore.ActionCore;
import com.sinopac.entity.RightsManagement.RolesVO;
import com.sinopac.model.RightsManagement.RightsManagementModel;

@Namespace("/RightsManagement")
@ParentPackage("struts-default")
public class RightsManagementAction extends ActionCore{

	private static final long serialVersionUID = 1L;
	private String loginId;
	//userList
	private List<RolesVO> userList;
	private List<RolesVO> adminList;
	private Boolean addFlag = false;
	private List<RolesVO> deleteList;
	//combobox
	private Map<String,String> comMap;
	//addUser
	private String user_roles;
	private String userName;
	private String passWord;
	private String userAccount;
	//login
	private Boolean loginFlag = false;
	//createAdmin
	private String adminId;
	private String adminName;
	private String adminPassword;
	private List<RolesVO> deleteAdmins;
	//updateAdminPassword
	private String adminNewPassword;
	private String adminNewPasswordConfirm;
	private Boolean updateFlag = false;
	private String originalPage = "";
	
	@Action(value="index",results={@Result(name="login",location="/form/RightsManagement/login.jsp"),
								   @Result(name="success",type="chain",location="userList")})
	public String login(){
		if(StringUtils.isNotBlank(userName) && StringUtils.isNotBlank(passWord)){
			RightsManagementModel rmm = new RightsManagementModel();
			boolean flag = rmm.chkUser(StringUtils.trimToEmpty(userName), StringUtils.trimToEmpty(passWord));
			if(flag){
				loginFlag = false;
				session.put("userName", userName);
				userName = "";
				passWord = "";
				return SUCCESS;
			}else{
				loginFlag = true;
				userName = "";
				passWord="";
				return LOGIN;
			}
		}else{
			loginFlag = false;
			userName = "";
			passWord="";
			return LOGIN;
		}
	}
	
	@Action(value="userList",results={@Result(name="success",location="/form/RightsManagement/amdForm.jsp")})
	public String userList(){
		userName = "";
		passWord = "";
		userList = new ArrayList<RolesVO>();
		RightsManagementModel rmm = new RightsManagementModel();
		userList = rmm.queryUserList("userList");
		adminList = rmm.queryUserList("sysList");
		setCombobox();
		return SUCCESS;
	}
	
	@Action(value="addUsers",results={@Result(name="success",type="chain",location="userList")})
	public String addUsers(){
		setCombobox();
		if(StringUtils.isNotBlank(userAccount)){
			RightsManagementModel rmm = new RightsManagementModel();
			boolean message = rmm.chkUserName(StringUtils.trimToEmpty(userAccount));
			if(message){//不可新增
				userAccount = "";
				userName = "";
				user_roles = "1";
				addFlag = true;
			}else{//可新增
				String userid = StringUtils.trimToEmpty(userAccount);
				String username = StringUtils.trimToEmpty(userName);
				String userroles = StringUtils.trimToEmpty(user_roles);
				rmm.createUser(userid, username, userroles);
				addFlag = false;
			}
		}else{
			userAccount = "";
			userName = "";
			user_roles = "1";
		}
		return SUCCESS;
	}
	
	@Action(value="createAdmin",results={@Result(name="success",type="chain",location="userList")})
	public String createAdmin(){
		if(StringUtils.isNotBlank(adminId)){
			RightsManagementModel rmm = new RightsManagementModel();
			boolean message = rmm.chkUserName(adminId);
			if(message){
				adminId = "";
				adminName = "";
				adminPassword = "";
				addFlag = true;
			}else{
				rmm.createAdmin(adminId.trim(), adminName.trim(), adminPassword.trim());
				addFlag = false;
			}
		}
		originalPage = "admin";
		return SUCCESS;
	}
	
	@Action(value="deleteUsers",results={@Result(name="success",type="chain",location="userList")})
	public String deleteUsers(){
		userList = new ArrayList<RolesVO>();
		RightsManagementModel rmm = new RightsManagementModel();
		userList = rmm.queryUserList("userList");
		for(int i=0;i<userList.size();i++){
			RolesVO dbVO = userList.get(i);
			RolesVO deleteVO = deleteList.get(i);
			if(!StringUtils.equals(dbVO.getUser_roles(), deleteVO.getUser_roles())){
				rmm.updateUserStatus(deleteVO.getUserid(), deleteVO.getUser_roles());
			}
			if(deleteVO.isDeleteFlag()){
				rmm.deleteUsers(deleteVO.getUserid());
			}
		}
		return SUCCESS;
	}
	
	@Action(value="deleteAdmins",results={@Result(name="success",type="chain",location="userList")})
	public String deleteAdmins(){
		RightsManagementModel rmm = new RightsManagementModel();
		for(int i=0;i<deleteAdmins.size();i++){
			RolesVO deleteVO = deleteAdmins.get(i);
			if(deleteVO.isDeleteFlag()){
				rmm.deleteUsers(deleteVO.getUserid());
			}
		}
		originalPage = "admin";
		return SUCCESS;
	}
	
	@Action(value="updateAdminPassword",results={@Result(name="success",type="chain",location="userList")})
	public String updateAdminPassword(){
		updateFlag = false;
		if(StringUtils.isNotBlank(adminPassword)){
			RightsManagementModel rmm = new RightsManagementModel();
			String userid = (String)session.get("userName");
			boolean flag = rmm.chkUser(StringUtils.trimToEmpty(userid), StringUtils.trimToEmpty(adminPassword));
			if(flag){
				if(!rmm.updatePassword(userid, adminNewPassword)){
					updateFlag = true;
				}
			}else{
				updateFlag = true;
			}
		}
		originalPage = "admin";
		return SUCCESS;
	}
	
	@Action(value="logout",results={@Result(name="logout",location="/form/RightsManagement/login.jsp")})
	public String logout(){
		session.clear();
		return LOGOUT;
	}
	
	/*================private Function====================*/
	
	private void setCombobox(){
		comMap = new LinkedHashMap<String,String>();
		comMap.put("1", "行員");
		comMap.put("2", "主管");
		comMap.put("3", "查詢");
		comMap.put("4", "前置");
	}

	public List<RolesVO> getUserList() {
		return userList;
	}

	public void setUserList(List<RolesVO> userList) {
		this.userList = new ArrayList<RolesVO>();
		this.userList = userList;
	}

	public Map<String, String> getComMap() {
		return comMap;
	}

	public void setComMap(Map<String, String> comMap) {
		this.comMap = comMap;
	}

	public String getUser_roles() {
		return user_roles;
	}

	public void setUser_roles(String user_roles) {
		this.user_roles = user_roles;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public Boolean getLoginFlag() {
		return loginFlag;
	}

	public void setLoginFlag(Boolean loginFlag) {
		this.loginFlag = loginFlag;
	}

	public String getUserAccount() {
		return userAccount;
	}

	public void setUserAccount(String userAccount) {
		this.userAccount = userAccount;
	}

	public Boolean getAddFlag() {
		return addFlag;
	}

	public void setAddFlag(Boolean addFlag) {
		this.addFlag = addFlag;
	}

	public List<RolesVO> getDeleteList() {
		return deleteList;
	}
	public void setDeleteList(List<RolesVO> deleteList) {
		this.deleteList = deleteList;
	}
	public String getAdminId() {
		return adminId;
	}
	public void setAdminId(String adminId) {
		this.adminId = adminId;
	}
	public String getAdminName() {
		return adminName;
	}
	public void setAdminName(String adminName) {
		this.adminName = adminName;
	}
	public String getAdminPassword() {
		return adminPassword;
	}
	public void setAdminPassword(String adminPassword) {
		this.adminPassword = adminPassword;
	}
	public List<RolesVO> getAdminList() {
		return adminList;
	}
	public void setAdminList(List<RolesVO> adminList) {
		this.adminList = adminList;
	}
	public List<RolesVO> getDeleteAdmins() {
		return deleteAdmins;
	}
	public void setDeleteAdmins(List<RolesVO> deleteAdmins) {
		this.deleteAdmins = deleteAdmins;
	}
	public String getLoginId() {
		return loginId;
	}
	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}
	public String getAdminNewPassword() {
		return adminNewPassword;
	}
	public void setAdminNewPassword(String adminNewPassword) {
		this.adminNewPassword = adminNewPassword;
	}
	public String getAdminNewPasswordConfirm() {
		return adminNewPasswordConfirm;
	}
	public void setAdminNewPasswordConfirm(String adminNewPasswordConfirm) {
		this.adminNewPasswordConfirm = adminNewPasswordConfirm;
	}
	public Boolean getUpdateFlag() {
		return updateFlag;
	}
	public void setUpdateFlag(Boolean updateFlag) {
		this.updateFlag = updateFlag;
	}

	public String getOriginalPage() {
		return originalPage;
	}

	public void setOriginalPage(String originalPage) {
		this.originalPage = originalPage;
	}


}
